<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="./lib/bootstrap-v4.6.0.css">
  <style>
    body {
      padding: 20px;
    }

    .loading-box {
      position: fixed;
      top: 30%;
      left: 50%;
      transform: translateX(-50%);
      background-color: rgba(0, 0, 0, 0.1);
      border-radius: 10px;
      box-shadow: 1px 1px 3px #efefef;
      display: none;
    }
  </style>
</head>

<body>
  <!-- 按钮 -->
  <button class="btn btn-primary" id="btnGET">GET</button>
  <button class="btn btn-info" id="btnPOST">POST</button>
  <button class="btn btn-warning" id="btnBooks">获取图书列表数据</button>

  <!-- loading 区域 -->
  <div class="loading-box">
    <div class="spinner-border m-5 text-primary" role="status">
      <span class="sr-only">Loading...</span>
    </div>
  </div>

  <script src="./lib/jquery-v3.6.0.js"></script>
  <script src="./lib/axios.js"></script>
  <script>
    // 配置请求根路径
    axios.defaults.baseURL = 'http://www.liulongbin.top:3009'

    $(function () {
      // 点击按钮，发起 GET 请求
      $('#btnGET').on('click', function () {
        // 展示 loading 效果
        $('.loading-box').show()
        axios.get('/api/get').then(function (res) {
          // 隐藏 loading 效果
          $('.loading-box').hide()
          console.log(res.data)
        })
      })

      // 点击按钮，发起 POST 请求
      $('#btnPOST').on('click', function () {
        // 展示 loading 效果
        $('.loading-box').show()
        axios.post('/api/post', { name: 'zs' }).then(function (res) {
          // 隐藏 loading 效果
          $('.loading-box').hide()
          console.log(res.data)
        })
      })

      $('#btnBooks').on('click', function () {
        // 展示 loading 效果
        $('.loading-box').show()
        axios.get('/api/getbooks').then(function (res) {
          // 隐藏 loading 效果
          $('.loading-box').hide()
          console.log(res.data)
        })
      })
    })
  </script>
</body>

</html>